home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / Amiga_Mail_Vol2 / Archives / Plain / mj90 / ASCII / StopwatchTimerIX-37.ascii < prev   
Encoding:
Text File  |  1993-12-07  |  4.9 KB  |  196 lines

  1. (c)  Copyright 1990 Commodore-Amiga, Inc.   All rights reserved.
  2. The information contained herein is subject to change without notice,
  3. and  is provided "as is" without warranty of any kind, either express
  4. or implied.   The entire risk as to the use of this information is
  5. assumed by the user.
  6.  
  7. Stopwatch Functions with the timer.device
  8.  
  9. by Mike Sinz
  10. To measure time on the Amiga the system software provides the
  11. timer.device.  The timer.device is very handy for many purposes,
  12. however, it does not have a built-in stopwatch.  It does have
  13. some time manipulation functions that make this a simple thing to
  14. do though.  This article shows how to do stopwatch functions
  15. using the timer.device.
  16.  
  17. The example code contains four routines needed to set up and use
  18. the stopwatch.
  19. The routines are:
  20.  
  21. o struct timerequest  *Init_Timer  (VOID)
  22. o VOID Timer_Start (struct timerequest *)
  23. o VOID Timer_Stop  (struct timerequest *)
  24. o VOID Free_Timer  (struct timerequest *)
  25.  
  26. The stopwatch functions all use a struct timerequest as their
  27. main parameter.  This is just the usual Amiga IORequest structure
  28. with two extra fields for seconds and microseconds.  Init_Timer()
  29. sets up the timer.device and returns a pointer to a struct
  30. timerequest.  This pointer is used in Timer_Start() and
  31. Timer_Stop() which measure the elapsed time.  Finally,
  32. Free_Timer() is used to close the timer.device and free the
  33. memory used for the timerequest.
  34.  
  35. An example main() function is listed below to show how to use the
  36. stopwatch functions to  measure elapsed time.  The example just
  37. does a Delay(73L) and then displays the amount of time that took. 
  38. Some uses for the program might include measuring how long a
  39. student takes to answer a question, the amount of time a player
  40. has been playing, or how fast the system is.  For more
  41. information on the timer.device, refer to the ROM Kernel Manual:
  42. Libraries and Devices (p. 871, ISBN 0-201-18187-8).
  43.  
  44.  
  45. /*
  46.  * Example stopwatch functions using the Amiga timer.device...
  47.  */
  48.  
  49. /*
  50.  * Makefile used to compile with Lattice C 5.04 or 5.05
  51.  *
  52.  
  53. #
  54. # MakeFile for StopWatch
  55. #
  56.  
  57. CFLAGS= -b1 -cfirstq -ms0 -rr1 -v -w
  58.  
  59. OBJS=   StopWatch.o
  60.  
  61. LIBS=   LIB:lcsr.lib
  62.  
  63. .c.o:
  64. @LC $(CFLAGS) $*
  65.  
  66. StopWatch: $(OBJS)
  67. @BLink FROM LIB:c.o $(OBJS) TO StopWatch LIB $(LIBS) SMALLDATA SMALLCODE
  68.  
  69.  *
  70.  *
  71.  */
  72.  
  73. #include    <exec/types.h>
  74. #include    <exec/memory.h>
  75. #include    <devices/timer.h>
  76.  
  77. #include    <proto/exec.h>
  78. #include    <proto/timer.h>
  79. #include    <proto/dos.h>
  80.  
  81. #include    <stdio.h>
  82.  
  83. /*
  84.  * The library base for the timer...
  85.  */
  86. struct  Library *TimerBase=NULL;
  87.  
  88. /*
  89.  * This gets the starting time...
  90.  */
  91. VOID Timer_Start(struct timerequest *Time_Req)
  92. {
  93.  
  94.     Time_Req->tr_node.io_Command=TR_GETSYSTIME;
  95.     Time_Req->tr_node.io_Flags=IOF_QUICK;
  96.     DoIO((struct IORequest *)Time_Req);
  97. }
  98.  
  99. /*
  100.  * This gets the ending time and computes the time difference
  101.  * in the timerequest->tr_time.
  102.  */
  103. VOID Timer_Stop(struct timerequest *Time_Req)
  104. {
  105. struct  timeval StartTime;
  106.  
  107.     StartTime=Time_Req->tr_time;
  108.  
  109.     Time_Req->tr_node.io_Command=TR_GETSYSTIME;
  110.     Time_Req->tr_node.io_Flags=IOF_QUICK;
  111.     DoIO((struct IORequest *)Time_Req);
  112.  
  113.     SubTime(&(Time_Req->tr_time),&StartTime);
  114. }
  115.  
  116. /*
  117.  * Initialize the stopwatch...
  118.  */
  119. struct timerequest *Init_Timer(VOID)
  120. {
  121. register    struct  timerequest *Time_Req=NULL;
  122. register    struct  MsgPort     *port=NULL;
  123.  
  124.  
  125.     if (port=CreatePort(NULL,NULL))
  126.     {
  127.         if (Time_Req=(struct timerequest *)CreateExtIO(port,
  128.                                                 sizeof(struct timerequest)))
  129.         {
  130.             if (!OpenDevice(TIMERNAME,UNIT_VBLANK,
  131.                             (struct IORequest *)Time_Req,NULL))
  132.             {
  133.                 TimerBase=(struct Library *)Time_Req->tr_node.io_Device;
  134.             }
  135.             else
  136.             {
  137.                 DeleteExtIO((struct IORequest *)Time_Req);
  138.                 Time_Req=NULL;
  139.             }
  140.         }
  141.         if (!Time_Req)
  142.         {
  143.             DeletePort(port);
  144.             port=NULL;
  145.         }
  146.     }
  147.  
  148.     return(Time_Req);
  149. }
  150.  
  151. /*
  152.  * Free up the timer...
  153.  */
  154. VOID Free_Timer(struct timerequest *Time_Req)
  155. {
  156.     if (Time_Req)
  157.     {
  158.         CloseDevice((struct IORequest *)Time_Req);
  159.         DeletePort(Time_Req->tr_node.io_Message.mn_ReplyPort);
  160.         DeleteExtIO((struct IORequest *)Time_Req);
  161.     }
  162. }
  163.  
  164.  
  165. /*
  166.  * A simple main() to use these features...
  167.  */
  168. VOID main(int argc, char *argv[])
  169. {
  170. register    struct  timerequest *StopWatch;
  171.  
  172.     if (argc)   /* Check if CLI... */
  173.     {
  174.         if (StopWatch=Init_Timer())
  175.         {
  176.             Timer_Start(StopWatch);
  177.  
  178.             /* Now we do something... */
  179.             Delay(73L);
  180.  
  181.             Timer_Stop(StopWatch);
  182.  
  183.             /* Now we display the results... */
  184.  
  185.             printf("Time taken: %ld.%06ld seconds\n",
  186.                         StopWatch->tr_time.tv_secs,
  187.                         StopWatch->tr_time.tv_micro);
  188.  
  189.             /* Free the structure again... */
  190.             Free_Timer(StopWatch);
  191.         }
  192.     }
  193. }
  194.  
  195.  
  196.